package com.example.androidpattern.observer.classes;

import com.example.androidpattern.observer.interfaces.Observer;
import com.example.androidpattern.observer.interfaces.Subject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Object3D$
 * @Description: 3D主题
 * @Author: xinya
 * @CreateDate: 2020/11/10$ 14:49$
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class Subject3D implements Subject {
    private List<Observer> mObserverList = new ArrayList<>();
    private String msg;

    @Override
    public void registerObserver(Observer observer) {
        if (!mObserverList.contains(observer)) {
            mObserverList.add(observer);
        }
    }

    @Override
    public void unregisterObserver(Observer observer) {
        mObserverList.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : mObserverList) {
            observer.update(this.msg);
        }
    }

    public void setMsg(String msg) {
        this.msg = msg;
        notifyObservers();
    }
}
